package binarySearch;

/*
* 前提：有序数组
* 二分查找
* */
public class BinarySearch {
    public static void main(String[] args) {
    	int[] nums = {1,2,3,4,5,6,7,8,9};
    	int i = searchOne(nums, 1, 0, 8);
    	System.out.println(i);
    }

    //假设升序 从小到大排序
    public static int searchOne(int[] nums,int target,int left,int right){
        int i =0;
        System.out.println("第"+i+"次调用");
        int mid = (left + right)/2;
        if (left > right || target <nums[left] || target > nums[right] ){
            return -1;
        }

        if (target > nums[mid]){
            //目标在中间值的右边  向右递归
            return searchOne(nums, target,mid+1,right);
        }else if(target < nums[mid]){
            return searchOne(nums,target,left,mid-1);
        }else{
            return nums[mid];
        }
    }
}
